package cn.edu.ctbu.myfirstweb.dao;

import cn.edu.ctbu.myfirstweb.domain.Student;
import cn.edu.ctbu.myfirstweb.domain.StudentScore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

public interface StudentRepository extends JpaRepository<Student, Long>,
        JpaSpecificationExecutor<StudentRepository>,
        Serializable {
    @Query(nativeQuery = true, value = "select cid1,score from tb_sc where sid = :id")
    List<Map<Long, Float>> getStudentScoreByID(@Param("id") Long id);

    @Query(nativeQuery = true, value = "select cid,cname,class1,credits,score from tb_sc,tb_course where sid = :id and cid1 = cid")
    List<StudentScore> getScore(@Param("id") Long id);

    Student findByName(String name);

    Student findByNumAndPassword(Long num, String password);

    Student findByNum(Long num);

    List<Student> findByNameLike(String name);

    @Modifying//通知SpringData这是一个update或delete操作
    @Transactional
//此操作需要使用事务
    void deleteByName(String name);//该方法返回类型为Long是因为此处返回的是删除的记录
}
//泛型中的Long为主键的类型